package com.caigou.security.config;

import com.caigou.bean.exam.entity.Users;
import com.caigou.security.utils.CurrentUser;
import com.caigou.service.IUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

@Component
public class PrincipalUsersConfig implements HandlerMethodArgumentResolver {

    @Autowired
    private IUsersService usersService;

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.getParameterAnnotation(CurrentUser.class) != null && parameter.getParameterType().equals(Users.class);
    }

    @Override
    public Users resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        return usersService.getCurrentUserInfo();
    }
}
